﻿@using Novagoal.Website.Models.Views
@using Novagoal.Models
@using Novagoal.Website.WebApp.Helpers
@using Novagoal.Models.Utils
@using Novagoal.Utils
@using Ids.Web.Security;

@model IndexViewModel
@{
    ViewBag.Title = "Grupos | Index";
    Layout = "~/Views/Shared/ListLayout.cshtml";
}

@section Messages {
}

@section ListTitle {
    Lista de Grupos
}

@section ListActions {
    @if (Model.List.HasFilters())
    {
        @Html.ActionLink("Mostrar todos", "Index", null, new { @class = "button ui-state-default ui-corner-all" } )
    }
    @if (Html.UserIsAtLeast(Enums.Roles.AdvancedUser))
    {
        @Html.ActionLink("Nuevo Grupo", "Add", null, new { @class = "button ui-state-default ui-corner-all" })
    }
}

@section ListContent {

    @Html.ValidationSummary(true)

    <table class="main-table">
        <tr>
            <th class="ui-widget-header ui-corner-all id">
                @ListHelper.SortAction(Url, Model.List, "Id", PartyListSortField.Id) <br />
                @ListHelper.TextBoxFor(Model.List, PartyListFilter.Id, "style=\"width:60px\"")
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Nombre", PartyListSortField.Name) <br />
                @ListHelper.TextBoxFor(Model.List, PartyListFilter.Name)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Torneo", PartyListSortField.Tournament) <br />
                @ListHelper.TextBoxFor(Model.List, PartyListFilter.Tournament)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Creado Por", PartyListSortField.CreatedBy) <br />
                @ListHelper.TextBoxFor(Model.List, PartyListFilter.CreatedBy)
            </th>
            <th class="ui-widget-header ui-corner-all">
                @ListHelper.SortAction(Url, Model.List, "Miembros", PartyListSortField.Participants) <br />
            </th>
            <th class="ui-widget-header ui-corner-all date-time">
                @ListHelper.SortAction(Url, Model.List, "Creado", PartyListSortField.CreatedAt) <br />
            </th>
            <th class="ui-widget-header ui-corner-all date-time">
                @ListHelper.SortAction(Url, Model.List, "Actualizado", PartyListSortField.UpdatedAt) <br />
            </th>
            <th class="ui-widget-header ui-corner-all actions" style="width:210px">Acciones</th>
        </tr>

        @for (int i = 0; i < Model.List.Items.Count; i++)
        {
            var party = Model.List.Items[i] as Party;
            <tr id='@party.Id'>
                <td class="center">
                    @party.Id
                </td>
                <td id="name-@party.Id">
                    <span id="name-span-@party.Id">@party.Name</span>
                </td>
                <td>
                    <span>@party.Tournament.Name</span>
                </td>
                <td>
                    <span>@party.CreatedBy.FullName</span>
                </td>
                <td class="center">
                    <span>@Model.Members.Where(p => p.Key == party.Id).Count()</span>
                </td>
                <td>
                    @if (party.CreatedAt != null)
                    {
                        @Html.Encode(String.Format("{0:g}", party.CreatedAt.ToLocalTime()))
                    }
                </td>
                <td>
                    @if (party.UpdatedAt != null)
                    {
                        @Html.Encode(String.Format("{0:g}", party.UpdatedAt.ToLocalTime()))
                    }
                </td>
                <td class="center" id="edit-@party.Id">
                    @if (Html.UserIsAtLeast(Enums.Roles.SuperUser) || party.CreatedBy.EmailAddress == User.Identity.Name)
                    {
                        <span id="edit-button-@party.Id" onclick="window.location='@Url.Action("Edit", "Parties", new { id = party.Id })'" class="button ui-state-default ui-corner-all">Editar</span>
                    }
                    
                    @if (Html.UserIsAtLeast(Enums.Roles.SuperUser) || party.CreatedBy.EmailAddress == User.Identity.Name || Model.Members.Any(p => p.Key == party.Id && p.Value.EmailAddress == User.Identity.Name))
                    {
                        <span id="view-button-@party.Id" onclick="window.location='@Url.Action("View", "Parties", new { id = party.Id })'" class="button ui-state-default ui-corner-all">Ver</span>
                    }
                    else
                    {
                        <span id="join-button-@party.Id" onclick="javascript:SendJoinRequest(@party.Id)" class="button ui-state-default ui-corner-all">Unirse</span>
                    }
                </td>
            </tr>
        }

    </table>

    <script type="text/javascript">
        function SendJoinRequest(partyId) {
            var $dialog = $('<div></div>')
		    .html("Seguro de enviar la solicitud de unirte a este grupo?")
		    .dialog({
		        autoOpen: false,
		        height: 150,
		        width: 300,
		        title: 'Confirmacion',
		        modal: true,
		        buttons: {
		            Ok: function() {
		                var url = '@Url.Action("SendJoinRequestMail", "Users")';
                        $.post(url, { partyId: partyId, email: '@User.Identity.Name'});
                        $(this).dialog("close");
	                },
                    Cancel: function () {
		                $(this).dialog("close");
		            }
		        }
            });
            $dialog.dialog('open');
        }
    </script>
}

@section ListPaging {
    @{
        @Html.DropDownListForPaging(4, 5, Model.RouteValues)
        string totalLabel = Model.List.TotalItems != 1 ? "grupos" : "grupo";
        @ListHelper.TotalItems(Model.List.TotalItems, totalLabel)
        @ListHelper.Paging(Url, Model.List)
    }
}

@section RecordEdition {
    
}


